package org.example.afree;

import java.util.ArrayList;
import java.util.HashSet;

public class Remove_duplicate_items_from_ordered_arrays {
    public static int removeDuplicates(int[] nums) {
        HashSet<Integer> hashSet = new HashSet<>();
        ArrayList<Integer> list = new ArrayList<>();
        hashSet.add(nums[0]);
        int res = 1;
        for (int i = 1; i < nums.length; i++) {
            if (!hashSet.contains(nums[i]) && list.size()!=0){
                hashSet.add(nums[i]);
                int tem = nums[i];
                nums[i] = nums[list.get(0)];
                nums[list.get(0)] = tem;
                list.remove(0);
                list.add(i);
                res++;
            }else if (hashSet.contains(nums[i])){
                list.add(i);
            }else if (!hashSet.contains(nums[i])&& list.size()==0){
                hashSet.add(nums[i]);
                res++;
            }
        }
        return res;
    }
}
